package com.cango.qutz.web.common;

import com.cango.framework.base.entity.SessionUserVo;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 *  session操作工具类<br> 
 * 〈功能详细描述〉
 *
 * @author weih
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
public class SessionUtils {
    private static final String USER_KEY_IN_SESSION = "web_user_key_in_session";


    /**
     * 功能描述: <br>
     * 得到session
     *
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    private static HttpSession getSession() {
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) ra).getRequest();
        return request.getSession();
    }

    public static String getFullDomain(){
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) ra).getRequest();
        return request.getScheme() + "://" + request.getServerName() + request.getContextPath();
    }

    /**
     * 功能描述: <br>
     * 得到session中的对象信息
     *
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static SessionUserVo getSessionUser() {
        /*if(null == user ){
            user = new SessionUserVo();
            user.setUserId(1L);
            user.setUserName("admin");
            user.setDeptId(1L);
            user.setOrgId(1L);
            user.setPassword("121");
            user.setPositionId(1L);
            user.setAgentNo("10001");
            user.setExtensionNo("80051");
        }*/
        return (SessionUserVo) getSession().getAttribute(USER_KEY_IN_SESSION);
    }

    /**
     * 功能描述: <br>
     * 设置当前session的用户信息
     *
     * @param user SessionUserVo
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static void setSessionUser(SessionUserVo user) {
        getSession().setAttribute(USER_KEY_IN_SESSION, user);
    }


    public static Object getSessionValue(String sessionKey) {
       return getSession().getAttribute(sessionKey);
    }

    public static void setSessionValue(String sessionKey, Object sessionValue) {
        getSession().setAttribute(sessionKey, sessionValue);
    }

    /**
     * 获取cookie
     */
    private static Cookie[] getCookies() {
        RequestAttributes ra = RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = ((ServletRequestAttributes) ra).getRequest();
        return request.getCookies();
    }

    /**
     * 获取cookie
     * @return
     */
    public static String getCookie(String name) {
        if(getCookies() == null){
            return null;
        }
        for (Cookie cookie: getCookies()){
            if(cookie.getName().equals(name)){
                return cookie.getValue();
            }
        }
        return null;
    }
}
